Create chart with multiple graphs overriding global aesthetics
Introduction
We have already covered how to create multiple graph charts by using global data here.
ggplot2 allows us to override the global aesthetics with local aesthetics for each geom function, which helps in customizing the charts further.
Procedure
We will be working with the MPG data set present in-built in ggplot2. Few of the rows of the dataset are as follows:
We have already looked at creating a multiple graph chart using global data here.
We will be customizing the aesthetics for a geom function which overrides the global aesthetics. The scatterplot is using the global aesthetics, we will override it such that the color of the scatter points map to the class of the car.
Code
library(ggplot2)
# overwrite global aesthetics with local aesthetics
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point(mapping = aes(color = class)) + geom_smooth()
And the output of above code is:
Conclusion
Thus we have successfully created chart with multiple graphs overriding global aesthetics.
References
- https://r4ds.had.co.nz/